package com.scedu.luckeymoney;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

@RestController
public class LuckymoneyController
{   @Autowired
    private LuckymoneyRepository repository;
    @Autowired
    private  LuckymoneyService luckymoneyService;
    /*
    获取红包列表
     */
    @GetMapping("/luckeymoenys")
    public List<Luckymoney> list(){
            return repository.findAll();
    }
    /*
    创建红包
     */
    @PostMapping("/luckymoneys")
    public  Luckymoney creat(@RequestParam("producer") String producer,
                             @RequestParam("money") BigDecimal money){
        Luckymoney luckymoney=new Luckymoney();
        luckymoney.setProducer(producer);
        luckymoney.setMoeny(money);
        return repository.save(luckymoney);

    }
    /*
    通过ID查询红包
     */
    @GetMapping("/luckymoneys/{id}")
    public Luckymoney findById(@PathVariable("id") Integer id){

        return repository.findById(id).orElse(null);
    }
    /*
    更新红包，领红包
     */
    @PutMapping("/luckymoneys/{id}")
     public Luckymoney UpdateById(@PathVariable("id") Integer id,
                                  @RequestParam("consumer") String consumer){
        Optional<Luckymoney> optional =repository.findById(id);
        if (optional.isPresent()){
            //如果有内容
            Luckymoney luckymoney=optional.get();
            luckymoney.setConsumer(consumer);
            return repository.save(luckymoney);
        }else{
           return  null;
        }


    }

    /*
    发两个红包
     */
    @PostMapping("/luckymoneys/two")
    public void createtwo(){
        luckymoneyService.createtwo();
    }
}
